package com.pcjavanet.generate;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ClassUtil {
	public static String getLowerStartName(String name){
		String rtvStr = name.substring(0,1).toLowerCase() + name.substring(1,name.length());
		return rtvStr;
	}
	
	public static  String getUpperStartName(String name){
		String rtv = name.substring(0,1).toUpperCase() + name.substring(1,name.length());
		return rtv;
	}
	
	public static String getBeanFullPackagePath(Class c){
		String name = c.getName().replace("class", "");
		return name;
	}
	
	public static boolean isReferenceModel( Field  field ){
		boolean rtv = false ; 
		if ( field.getName().length()>=3 && field.getName().substring(0, 3).equalsIgnoreCase("ref")){
			rtv = true ; 
		}
		return rtv;
	}
	
	public static Field[]  getAllRefFields( Field[] fields ) {
		List<Field> fs = new ArrayList<Field>();
		for( int i = 0 ; i < fields.length ; i++ ) {
			Field f = fields[i];
			if ( ClassUtil.isReferenceModel(f) == true  )
				fs.add(f);
		}
		return  fs.toArray(  new Field[ fs.size()] );
	}
	
	public static  String[] getRefBeansByRefFields( Field[] refFields ) {
		String[] rtvs = new String[ refFields.length ];
		for( int i = 0 ; i < rtvs.length ; i++ ){
			rtvs[i] = getBeanNameFromRefFieldName( refFields[i].getName() );
		}
		return rtvs;
	}
	public static Field[]  getAllNoRefFields( Field[] fields ) {
		List<Field> fs = new ArrayList<Field>();
		for( int i = 0 ; i < fields.length ; i++ ) {
			Field f = fields[i];
			if ( ClassUtil.isReferenceModel(f) == false   )
				fs.add(f);
		}
		return fs.toArray( new Field[ fs.size()]);
	}
	
	public static String getBeanNameFromRefFieldName(String fieldName){
		int beanIndex = 0;
		int beanEnd = 0;
		String beanName = "";
		for (int j = 0; j < fieldName.length(); j++) {
			char c = fieldName.charAt(j);
			// A =65 Z=90 a=97 z=122
			if (c >= 65 && c <= 90) {
				if (beanIndex == 0) {
					beanIndex = j;
					continue;
				}
				if (beanIndex != 0 && beanEnd == 0) {
					beanEnd = j;
					break;
				}
			}
		}
		if (beanIndex == 0 || (beanIndex != 0 && beanEnd == 0)) {
			System.err.print("taken error :" + fieldName);
		} else {
			beanName = fieldName.substring(beanIndex, beanEnd);
		}
		return beanName; 
	}
	
	public static List<String> getBeanNames(List<String> refFieldNames){
		List<String> rtvs = new ArrayList<String>();
		for( int i = 0 ; i < refFieldNames.size() ; i++ ) {
			String fieldName = refFieldNames.get(i);
			String beanName = getBeanNameFromRefFieldName( fieldName);
			if (! beanName .equals(""))
				rtvs.add( beanName );
		}
		return rtvs;
	}
	
	public static List<String> getBeanNamesByFields(List<Field> refFieldNames){
		List<String> rtvs = new ArrayList<String>();
		for( int i = 0 ; i < refFieldNames.size() ; i++ ) {
			String fieldName = refFieldNames.get(i).getName();
			String beanName = getBeanNameFromRefFieldName( fieldName);
			if (! beanName .equals(""))
				rtvs.add( beanName );
			
		}
		return rtvs;
	}
	
	public static boolean isReferenceModel( Field[] fields){
		boolean rtv  = false ;
		for( int i = 0; i < fields.length ; i++ ){
			if ( ClassUtil.isReferenceModel(fields[i]) ){
				rtv = true ; 
				break; 
			}
		}
		return rtv; 
	}
	public static void main(String args[]){
		String name = "Simple";
		String name2 = "jack";
		System.out.println( getLowerStartName(name) );
		System.out.println( getUpperStartName(name2) );
	}
}
